package com.kmproject.haifu.mvp.ui.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.kmproject.haifu.mvp.model.entity.MessageEvent;
import com.weigan.loopview.LoopView;

import java.util.ArrayList;
import java.util.List;

import com.kmproject.haifu.R;


/**
 * ================================================
 * 作    者：JayGoo
 * 版    本：1.1.0
 * 创建日期：2017/2/22
 * 描    述:
 * ================================================
 */
public class MultiSelectPopWindow {
    private RelativeLayout.LayoutParams layoutParams;
    private PopupWindow mPopupWindow;
    private TextView cancelBtn;
    private TextView confirmBtn;
    private TextView titleTV;
    private LoopView loopView;
    private OnConfirmClickListener mOnConfirmListener;
    private ArrayList<Integer> mIndexList;
    private Builder mBuilder;
    private RelativeLayout rootview1, rootview2, rootview3;


    public interface OnConfirmClickListener {
        void onClick(MessageEvent messageEvent);
    }

    static public class Builder {
        private Activity mActivity;
        private List<String> choiceNameList = new ArrayList<>();
        private String title;
        private String confirmText;
        private String cancelText;
        private boolean isOutsideTouchable;
        private View.OnClickListener mOnCancelListener;
        private OnConfirmClickListener mOnConfirmListener;
        private int mConfirmTextColor;
        private int mCancelTextColor;
        private int mTitleTextColor;

        private int defultIndex = 0;

        public Builder(Activity mActivity) {
            this.mActivity = mActivity;
        }


        public Builder setDataArray(List<String> list) {
            this.choiceNameList = list;
            return this;
        }


        /**
         * set title
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * set confirm button text
         *
         * @param str
         * @return
         */
        public Builder setConfirm(String str) {
            this.confirmText = str;
            return this;
        }

        /**
         * set cacel button text
         *
         * @param str
         * @return
         */
        public Builder setCancel(String str) {
            this.cancelText = str;
            return this;
        }

        /**
         * set title's text color
         *
         * @param color
         * @return
         */
        public Builder setTitleTextColor(int color) {
            this.mTitleTextColor = color;
            return this;
        }

        /**
         * 设置默认选中
         *
         * @param defultIndex
         * @return
         */
        public Builder setDefultSelect(int defultIndex) {
            this.defultIndex = defultIndex;
            return this;
        }

        /**
         * set confirm button's text color
         *
         * @param color
         * @return
         */
        public Builder setConfirmTextColor(int color) {
            this.mConfirmTextColor = color;
            return this;
        }

        /**
         * set cancel button's text color
         *
         * @param color
         * @return
         */
        public Builder setCancelTextColor(int color) {
            this.mCancelTextColor = color;
            return this;
        }

        /**
         * set if can touchable if your finger touch outside
         *
         * @param isOutsideTouchable
         * @return
         */
        public Builder setOutsideTouchable(boolean isOutsideTouchable) {
            this.isOutsideTouchable = isOutsideTouchable;
            return this;
        }

        public MultiSelectPopWindow build() {
            return new MultiSelectPopWindow(this);
        }

        public Builder setConfirmListener(OnConfirmClickListener listener) {
            this.mOnConfirmListener = listener;
            return this;
        }

        public Builder setCancelListener(View.OnClickListener listener) {
            this.mOnCancelListener = listener;
            return this;
        }


    }

    private MultiSelectPopWindow(final Builder builder) {
        mBuilder = builder;
        //init PopWindow
        View popview = View.inflate(builder.mActivity, R.layout.multi_select_list_popwindow, null);
        mPopupWindow = new PopupWindow(popview, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(builder.isOutsideTouchable);
        initViews(mPopupWindow.getContentView());
        initListener();
        showloopView(mBuilder.choiceNameList, mBuilder.defultIndex);
    }

    /**
     * init listener
     */
    private void initListener() {
        this.mOnConfirmListener = mBuilder.mOnConfirmListener;
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnConfirmListener.onClick(new MessageEvent(mBuilder.choiceNameList.get(loopView.getSelectedItem()), loopView.getSelectedItem()));
                dismiss();
            }
        });


        if (mBuilder.mOnCancelListener != null) {
            cancelBtn.setOnClickListener(mBuilder.mOnCancelListener);
        }

        cancelBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                dismiss();
                return false;
            }
        });
    }

    private void initViews(View root) {
        titleTV = (TextView) root.findViewById(R.id.title);
        cancelBtn = (TextView) root.findViewById(R.id.cancelBtn);
        confirmBtn = (TextView) root.findViewById(R.id.confirmBtn);


        setText(titleTV, mBuilder.title);
        setText(cancelBtn, mBuilder.cancelText);
        setText(confirmBtn, mBuilder.confirmText);

        setTextColor(titleTV, mBuilder.mTitleTextColor);
        setTextColor(cancelBtn, mBuilder.mCancelTextColor);
        setTextColor(confirmBtn, mBuilder.mConfirmTextColor);
        loopView = (LoopView) root.findViewById(R.id.type_loopview);

    }


    /**
     * 滚动选择
     */
    public void showloopView(List<String> list, int defultIndex) {
        loopView.setNotLoop();
        loopView.setItems(list);
        loopView.setInitPosition(defultIndex);

    }

    private void setText(TextView tv, String str) {
        if (tv != null && str != null) {
            tv.setText(str);
        }
    }

    private void setTextColor(TextView tv, int color) {
        if (tv != null && color != 0) {
            tv.setTextColor(color);
        }
    }

    public void dismiss() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
    }

    /**
     * parent is the popwindow show location
     *
     * @param parent
     */
    public void show(View parent) {
        if (mPopupWindow != null) {
            backgroundAlpha(0.8f);
            mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * set background alpha
     *
     * @param alpha
     */
    public void backgroundAlpha(float alpha) {
        try {
            WindowManager.LayoutParams lp = mBuilder.mActivity.getWindow().getAttributes();
            lp.alpha = alpha; //0.0-1.0
            mBuilder.mActivity.getWindow().setAttributes(lp);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
